/*
    Copyright 2006 Andrew Wilkinson <andrew@indiegigs.co.uk>

    This file is part of GL2x.

    GL2x is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    GL2x is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with GL2x; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

#include <stdlib.h>
#include <stdio.h>

#define CLIENT
#include "gl_internal.h"

struct state* current_state;

struct state* newstate() {
    struct state* r = (struct state*)malloc(sizeof(struct state));

    r->error = GL_NO_ERROR;
    r->begin = 0;
    r->glpoly = 0;

    r->clearcolour.r = Fixed_zero;
    r->clearcolour.g = Fixed_zero;
    r->clearcolour.b = Fixed_zero;
    r->clearcolour.a = Fixed_one;

    r->colour.r = Fixed_one;
    r->colour.g = Fixed_one;
    r->colour.b = Fixed_one;
    r->colour.a = Fixed_one;

    r->stack_type = GL_MODELVIEW;

    r->mvs = malloc(sizeof(matrixstack));
    r->mvs->m = Matrix_identity();
    r->mvs->next = 0;

    r->pjs = malloc(sizeof(matrixstack));
    r->pjs->m = Matrix_identity();
    r->pjs->next = 0;

    r->cull_face = GL_BACK;

    r->texture_env_mode = GL_MODULATE;
    r->texture_env_color.r = Fixed_zero;
    r->texture_env_color.g = Fixed_zero;
    r->texture_env_color.b = Fixed_zero;
    r->texture_env_color.a = Fixed_one;

    r->viewport_x = 0;
    r->viewport_y = 0;
    r->viewport_half_height = 320/2;
    r->viewport_half_width = 240/2;

    return r;
}

void deletestate(struct state* s) {
    if(s->glpoly) {
        fprintf(stderr, "Error! A polygon was not properly deleted from memory. Missing glEnd()?\n");
    }

    free(s);
}
